package com.smxy.daydayup.util.tree;

import com.smxy.daydayup.pojo.SectionTree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

/**
 * @team daydayup
 * @time 2021-12-12 8:53
 */

// 树状目录工具类
public class TreeUtils {

    // 生成el-tree格式的目录
    public static <T extends DataTree<T>> List<T> getTreeList(List<T> entityList) {

        List<T> resultList = new ArrayList<>();
        Map<Object, T> treeMap = new HashMap<>();
        T itemTree;

        for (int i = 0; i < entityList.size() && !entityList.isEmpty(); i++) {
            itemTree = entityList.get(i);
            //把所有的数据放到treeMap中，id为key
            treeMap.put(itemTree.getId(), itemTree);
            //把顶层节点放到集合resultList中
            if (itemTree.getParentId() == null) {
                resultList.add(itemTree);
            }
        }
        //循环数据，把数据放到上一级的children属性中
        for (int i = 0; i < entityList.size() && !entityList.isEmpty(); i++) {
            itemTree = entityList.get(i);
            T data = treeMap.get(itemTree.getParentId());
            // 不等于null，也就意味着有父节点
            if (data != null) {
                if (data.getChildren() == null) {
                    data.setChildren(new ArrayList<>());
                }
                //把子节点 放到父节点Children当中
                data.getChildren().add(itemTree);
                //把放好的数据放回map当中
                treeMap.put(itemTree.getParentId(), data);
            }
        }
        return resultList;
    }

    //解析el-tree返回的树
    public static List<SectionTree> resolveTree(List<SectionTree> treeList){
        if(treeList==null){
            return null;
        }

        List<SectionTree> resultList = new ArrayList<>();
        Queue<SectionTree> queue = new LinkedList<>();

        int c = 0;
        for(int i=0;i<treeList.size();i++){
            queue.add(treeList.get(i));
            while (!queue.isEmpty()){
                SectionTree child = queue.poll();
                child.setId(++c);
                resultList.add(child);
                if(child.getChildren()!=null){
                    for(int j=0;j<child.getChildren().size();j++){
                        SectionTree now = child.getChildren().get(j);
                        now.setParentId(child.getId());
                        queue.add(now);
                    }
                } else if (child.getParentId() == null) {
                    SectionTree defaultChild = new SectionTree();
                    defaultChild.setParentId(child.getId());
                    defaultChild.setLabel("知识点一概要");
                    queue.add(defaultChild);
                }
            }
        }

        return resultList;
    }

    public static List<SectionTree> constructInitTreeByResolve(){
        List<SectionTree> initTree = new ArrayList<>();

        String[] topTitles = new String[]{"点击展开课时列表","历史","核心概念","扩展知识"};
        for (String topTitle : topTitles) {
            SectionTree lv1 = new SectionTree();
            lv1.setLabel(topTitle);

            ArrayList<SectionTree> children = new ArrayList<>();

            children.add(new SectionTree(null,null,"知识点一概述",null,"",null));
            children.add(new SectionTree(null,null,"知识点二概述",null,"",null));
            children.add(new SectionTree(null,null,"知识点三概述",null,"",null));
            children.add(new SectionTree(null,null,"知识点四概述",null,"",null));
            lv1.setChildren(children);
            initTree.add(lv1);
        }
        return resolveTree(initTree);

    }

    // 生成章节初始树目录
    public static List<SectionTree> constructInitTree(Integer courseId){
        List<SectionTree> initTree = new ArrayList<>();

        // 一级目录
        initTree.add(new SectionTree(null,1,"第1课时 概述",null,null,courseId));
        initTree.add(new SectionTree(null,2,"第2课时 历史",null,null,courseId));
        initTree.add(new SectionTree(null,3,"第3课时 核心概念",null,null,courseId));
        initTree.add(new SectionTree(null,4,"第4课时 扩展知识",null,null,courseId));

        //次级目录
        initTree.add(new SectionTree(null,5,"1.1 知识点一",1,null,courseId));
        initTree.add(new SectionTree(null,6,"1.2 知识点二",1,null,courseId));
        initTree.add(new SectionTree(null,7,"1.3 知识点三",1,null,courseId));
        initTree.add(new SectionTree(null,8,"1.4 知识点四",1,null,courseId));

        initTree.add(new SectionTree(null,9,"2.1 知识点一",2,null,courseId));
        initTree.add(new SectionTree(null,10,"2.2 知识点二",2,null,courseId));
        initTree.add(new SectionTree(null,11,"2.3 知识点三",2,null,courseId));
        initTree.add(new SectionTree(null,12,"2.4 知识点四",2,null,courseId));

        initTree.add(new SectionTree(null,13,"3.1 知识点一",3,null,courseId));
        initTree.add(new SectionTree(null,14,"3.2 知识点二",3,null,courseId));
        initTree.add(new SectionTree(null,15,"3.3 知识点三",3,null,courseId));
        initTree.add(new SectionTree(null,16,"3.4 知识点四",3,null,courseId));

        initTree.add(new SectionTree(null,17,"4.1 知识点一",4,null,courseId));
        initTree.add(new SectionTree(null,18,"4.2 知识点二",4,null,courseId));
        initTree.add(new SectionTree(null,19,"4.3 知识点三",4,null,courseId));
        initTree.add(new SectionTree(null,20,"4.4 知识点四",4,null,courseId));

        return initTree;
    }
}
